import {
	getDateString
} from "@/assets/js/common.js";
let Controller = {
	mounted() {},
	activated() {
		this.requestTableList();
		// this.getDowmList();
	},
	methods: {
		onBalancePrint(){
			this.$confirm('请认真核对结算单金额，一旦确定财务将以该结算单进行结算。', '提示', {
				confirmButtonText: '同意',
				cancelButtonText: '取消',
			}).then(()=>{
				
			}).catch(err=>{})
		},
		onDelivery(barCode) {
			if (barCode) {
				this.$confirm('是否确认发货?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					this.$ajax
						.post(this.apiTest + `/supplierGoods/dispatchGoodsList`, params)
						.then(response => {
							const data = response.data;
							this.Model.tableData = data.result.result;
							this.Model.pager.pageTotal = data.result.total;
							this.Model.pager.pageIndex = data.result.pageIndex;
						})
						.catch(error => {
							this.$message.error('请求错误');
						});
				}).catch(err => {})
			} else {
				const list = this.Model.selectionList;
				if (list.length == 0) {
					this.$message.warning('请勾选至少1条!')
				} else {
					this.$confirm(`已选商品数量${list.length}件，是否批量发货？`, '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
					}).then(() => {
						this.$ajax
							.post(this.apiTest + `/supplier/whWarehouse/list`, params)
							.then(response => {
								const data = response.data;
								if (data.status == 200) {
									this.$message({
										type: 'success',
										message: `成功送货${list.length}件！送货单唯一号：390393847`
									})
								}
							})
							.catch(error => {
								this.$message.error('请求错误');
							});
					}).catch(err => {})
				}
		
			}
		
		},
		onTableCreate(id) {
			//跳转新建或编辑
			const name = 'createPurchaseOrder';
			this.$router.push({
				name: name,
				query: {
					data: JSON.stringify({
						id
					}),
				}
			})
		},
		onTableExport() {
			const arr = this.Model.selectionList;
			if (arr.length == 0) {
				this.$message.warning('请勾选需要导出的数据！')
				return;
			};
			const param = {
				name: 'purchaseStorage',
				idList: arr
			};
			this.$ajax({
				method: 'post',
				url: this.apiTest + `/supplier/common/exportExcel`,
				data: param,
				responseType: 'arraybuffer'
			}).then(res => {
				const data = res.data;
				let blob = new Blob([res.data], {
					type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
				});
				let objectUrl = URL.createObjectURL(blob);
				window.open(objectUrl);
			})
			/* this.$ajax.post(this.apiTest + `/supplier/common/exportExcel`,param)
			  .then(res => {
			    const data = res.data;
			    if(data.status == 200){
			      this.downloadExcel();
			    }
			  }).catch(err => {
			    this.$message.error('导出失败!')
			  }) */
		},
		onReceive(barCode) {
			this.$confirm('是否确认收货?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax
					.post(this.apiTest + `/supplier/whWarehouse/list`, params)
					.then(response => {
						const data = response.data;
						this.Model.tableData = data.result.result;
						this.Model.pager.pageTotal = data.result.total;
						this.Model.pager.pageIndex = data.result.pageIndex;
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}).catch(err => {})

		},
		getDowmList() {
			this.$ajax.post(this.apiTest + `/supplier/whWarehouse/getDropBox/204`, {})
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						const result = data.result;
						this.Model.filterOptionData = result;
						let locationArr = [],
							wareHouseStatusArr = [],
							wareHouseTypeArr = [],
							supplierArr = [];
						for (let i in result.location) { //入库仓库
							locationArr.push({
								label: result.location[i],
								value: i
							});
						};
						for (let i in result.wareHouseStatus) { //入库单状态
							wareHouseStatusArr.push({
								label: result.wareHouseStatus[i],
								value: i
							});
						};
						for (let i in result.wareHouseType) { //入库单类型
							wareHouseTypeArr.push({
								label: result.wareHouseType[i],
								value: i
							});
						};
						for (let i in result.supplier) { //入库单类型
							supplierArr.push({
								label: result.supplier[i],
								value: i
							});
						};
						this.Model.filterOption.items[4].data = supplierArr; //供应商
						this.Model.filterOption.items[3].data = locationArr; //入库仓库
						this.Model.filterOption.items[2].data = wareHouseStatusArr; //入库单状态
						this.Model.filterOption.items[1].data = wareHouseTypeArr; //入库单类型
						this.Model.defaultFilterOption = _.cloneDeep(this.Model.filterOption)
					}
				})
		},
		//请求主表数据
		requestTableList() {
			const params = {
				condition: {
					...this.getSearchInfoParam(),
				},
				...this.Model.pager.params,
			};
			this.$ajax.post(`${api.supplier}/whWarehouseArrival/settlement/list`, params).then(response => {
				const data = response.data;
				this.Model.tableData = data.result.result;
				this.Model.pager.pageTotal = data.result.total;
				this.Model.pager.pageIndex = data.result.pageIndex;
			}).catch(error => {
				this.$message.error('请求错误');
			});
		},
		getSearchInfoParam() {
			//编辑高级搜索时间格式
			let data = _.cloneDeep(this.Model.searchFromData);
			if (data.deliveryTime) {
				data.deliveryStartTime = getDateString(data.deliveryTime[0]);
				data.deliveryEndTime = getDateString(data.deliveryTime[1]);
				data.deliveryTime = undefined;
			}
			return data;
		},
		toTableDetail(data,name) {
			//跳转入库详情
			this.$router.push({
				name: name,
				query: {settlementNumber: data.settlementNumber, id: data.id},
			})
		},
		onTableSelectionChange(val) {
			//表格多选操作
			if (val.length == 0) {
				this.Model.selectionList = [];
				return;
			}
			let arr = val.map((item, index, list) => {
				return item.id
			})
			this.Model.selectionList = arr;
		},
		onTableCurrentChange(val) {
			//表格分页-当前页变动操作
			this.Model.pager.params.pageIndex = val;
			this.requestTableList();
		},
		onTableSizeChange(val) {
			//表格分页-页数量变动操作
			this.Model.pager.params.pageSize = val;
			this.requestTableList();
		},
		onCardClick(color) {
			//页面card搜索点击操作
			this.Model.cardSelected = color;
		},
		getFilterData(val, item, option) {
			/* 
			 saleOrderStatus订单状态[1]
			 saleOrderTypes售中类型-状态联动[2][3]
			 saleOrderChannels销售渠道[4][5]
			 deliveryTypes发货方式[6]
			 */
			// this.Model.filterOption.items = _.cloneDeep(option);
			for (let i = 0; i < 5; i++) {
				this.Model.filterOption.items[i] = option[i];
			}
			switch (item.key) {
				case 'saleOrderChannels':
					this.Model.filterOption.items[5].data = this.Model.saleManList[item.val].map(obj => {
						return {
							label: obj.value,
							value: obj.key
						}
					});
					this.Model.filterOption.items[5].value = undefined;
					break;
				case 'saleOrderTypes':
					this.Model.filterOption.items[3].data = this.Model.saleAfterOrderStatus[item.val].map(obj => {
						return {
							label: obj.value,
							value: obj.key
						}
					});
					this.Model.filterOption.items[3].value = undefined;
					break;
				default:
					break;
			};
			this.Model.searchFromData = val;
		},
		onFilterSearch(val) {
			this.requestTableList();
		},
		onFilterClear(val) {
			this.Model.searchFromData = _.cloneDeep(this.Model.defaultSearchFromData);
			this.Model.filterOption = _.cloneDeep(this.Model.defaultFilterOption);
		},
		onFilterExport(val) {},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		setPage(pageIndex, pageSize, pageTotal, total) {
			this.Model.pager.params = {
				pageIndex,
				pageSize
			};
			this.Model.pager.total = total;
		},
		getStatusText(type, value) {
			let text = '';
			switch (type) {
				case 'payStatus':
					/* 0未支付/1部分支付/2已支付 */
					switch (value) {
						case 0:
							text = '未支付';
							break;
						case 1:
							text = '部分支付';
							break;
						case 2:
							text = '已支付';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'orderStatus':
					/* 0待付款/1待审核/2待发货/3待提货/4部分已发货/5待收货/6已完成/7已关闭 */
					switch (value) {
						case 0:
							text = '待付款';
							break;
						case 1:
							text = '待审核';
							break;
						case 2:
							text = '待发货';
							break;
						case 3:
							text = '待提货';
							break;
						case 4:
							text = '部分已发货';
							break;
						case 5:
							text = '待收货';
							break;
						case 6:
							text = '已完成';
							break;
						case 7:
							text = '已关闭';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'aftersaleStatus':
					/* 
					  售后：0退款中/1退款成功/2退款拒绝/ 
					  售中：3待审核/4已取消/5变更成功/6待用户支付/7待平台退款
					 */
					switch (value) {
						case 0:
							text = '退款中';
							break;
						case 1:
							text = '退款成功';
							break;
						case 2:
							text = '退款拒绝';
							break;
						case 3:
							text = '待审核';
							break;
						case 4:
							text = '已取消';
							break;
						case 5:
							text = '变更成功';
							break;
						case 6:
							text = '待用户支付';
							break;
						case 7:
							text = '待平台退款';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'orderType':
					/* 10现货订单/20定制订单/30售中订单/40售后订单 */
					switch (value) {
						case 10:
							text = '现货订单';
							break;
						case 20:
							text = '定制订单';
							break;
						case 30:
							text = '售中订单';
							break;
						case 40:
							text = '售后订单';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'afterSaleType':
					/* （10售中变更 20退款 30升级换款 40常规售后） */
					switch (value) {
						case 10:
							text = '售中变更';
							break;
						case 20:
							text = '退款';
							break;
						case 30:
							text = '升级换款';
							break;
						case 40:
							text = '常规售后';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				default:
					break;
			}
			return text;
		},
		cancelInSale(id) {
			this.$confirm('是否确认取消售中?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax
					.get(this.apiTest + `/sale/onSale/order/sale/cancel/${id}`)
					.then(response => {
						const data = response.data;
						if (data.status == 200 && data.result == 1) {
							this.$message({
								type: 'success',
								message: '已成功'
							});
							this.requestTableList();
						};
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}).catch(() => {

			});

		},
		cancelRefund(id) {
			this.$confirm('是否确认取消退款?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax
					.get(this.apiTest + `/sale/onSale/order/refund/cancel/${id}`)
					.then(response => {
						const data = response.data;
						if (data.status == 200 && data.result == 1) {
							this.$message({
								type: 'success',
								message: '已成功'
							});
							this.requestTableList();
						};
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}).catch(() => {

			});

		},
		sendlRefund(id) {
			this.$ajax
				.get(this.apiTest + `/sale/onSale/order/refund/Initiate/${id}`)
				.then(response => {
					const data = response.data;
					if (data.status == 200 && data.result == 1) {
						this.$message({
							type: 'success',
							message: '已成功'
						});
					}
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		showPayDialog(id) {
			this.Model.dialogFormVisible = true;
			this.Model.nowSelectId = id;
		},
		pay() {
			const form = this.Model.dialogForm;
			const params = {
				payMethod: form.payMethod,
				paymentMoney: form.paymentMoney,
				serialNumber: form.serialNumber,
				payTime: getDateString(form.payTime),
				orderId: this.Model.nowSelectId
			};
			this.$ajax
				.post(this.apiTest + `/sale/onSale/order/afterSale/pay`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.$message({
							type: 'success',
							message: '已成功'
						});
						this.requestTableList();
					};
					this.Model.dialogFormVisible = false;
				})
				.catch(error => {
					this.$message.error('请求错误');
					this.Model.dialogFormVisible = false;
				});
		},
	}
}
export default Controller;
